package org.sgroup.imenu.controladapter;

import java.util.List;
import org.sgroup.imenu.bo.Category;
import org.sgroup.imenu.util.LoadBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoriesListAdapter extends ArrayAdapter<Category> {

	Context context;
	int resourceid;
	List<Category> objects;

	public CategoriesListAdapter(Context context, int resource,
			List<Category> objects) {
		super(context, resource, objects);
		this.context = context;
		this.resourceid = resource;
		this.objects = objects;
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater flater = LayoutInflater.from(context);
			convertView = flater.inflate(resourceid, null);
		}
		TextView catName = (TextView) convertView
				.findViewById(org.sgroup.imenu.R.id.list_category_text);
		ImageView catIgm = (ImageView) convertView
				.findViewById(org.sgroup.imenu.R.id.list_category_icon);

		Category currentObj = objects.get(position);
		catName.setText(currentObj.Name);
		if (!currentObj.ImageFile.isEmpty()) {
			try {
				Bitmap bm = LoadBitmap.GetBitmap(this.context,
						currentObj.ImageFile);
				catIgm.setImageBitmap(bm);
			} catch (Exception e) {
				Log.v("CategoriesListAdapter", e.getMessage());
			}
		} else {
			try {
				catIgm.setImageResource(org.sgroup.imenu.R.drawable.ic_launcher);
			} catch (Exception e) {
				Log.v("CategoriesListAdapter", e.getMessage());
			}			
		}
		return convertView;
	}

}
